package com.common.facebody.service;

import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.facebody.model.v20191230.*;
import com.common.facebody.exception.FaceBodyBaseException;
import com.common.facebody.exception.FaceBodyExceptionMsg;
import com.common.facebody.instance.FaceBodyInstance;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 牧鱼
 * @Classname FaceBodyService
 * @Description TODO
 * @Date 2021/4/25
 */
@Service
public class FaceBodyService {

    private IAcsClient client ;

    private Float faceBodyScore ;

    /**
     * 实例化人脸识别对象
     * @param faceBodyInstance
     */
    public FaceBodyService(FaceBodyInstance faceBodyInstance) {
        this.client = faceBodyInstance.getClient();
        this.faceBodyScore = faceBodyInstance.getFaceBodyScore();
    }

    /**
     * 创建人脸数据库
     * 阿里云平台可能会默认创建一个名为default的数据库，可以删除掉再建立新的。但只能拥有一个数据库
     *
     * @param dbName
     */
    public CreateFaceDbResponse createFaceDb(String dbName) {
        CreateFaceDbResponse response = null;
        CreateFaceDbRequest request = new CreateFaceDbRequest();
        request.setName(dbName);
        try {
            response = client.getAcsResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        }
        return response;
    }

    /**
     * 添加人脸样本
     *
     * @param dbName
     * @param label
     */
    public AddFaceEntityResponse addFaceEntity(String dbName, String label) {
        AddFaceEntityRequest request = new AddFaceEntityRequest();
        AddFaceEntityResponse response = null;
        request.setDbName(dbName);
        request.setEntityId(UUID.randomUUID().toString().replaceAll("-", ""));
        request.setLabels(label);
        try {
            response = client.getAcsResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        }
        return response;
    }

    /**
     * 添加人脸数据
     *
     * @param dbName
     * @param imageUrl
     * @param entityId
     * @param extraData
     */
    public AddFaceResponse addFace(String dbName, String imageUrl, String entityId, String extraData) {
        AddFaceRequest request = new AddFaceRequest();
        AddFaceResponse response = null;
        request.setDbName(dbName);
        request.setImageUrl(imageUrl);
        request.setEntityId(entityId);
        request.setExtraData(extraData);
        try {
            response = client.getAcsResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        }
        return response;
    }

    /**
     * 活体检测  检测是否是直接拍摄的人脸图片  还是翻拍的图片
     *
     * @param imageUrl
     */
    public DetectLivingFaceResponse detectLiving(String imageUrl) {
        DetectLivingFaceRequest request = new DetectLivingFaceRequest();
        DetectLivingFaceResponse response = null;
        try {
            List<DetectLivingFaceRequest.Tasks> tasksList = new ArrayList<DetectLivingFaceRequest.Tasks>();
            DetectLivingFaceRequest.Tasks tasks1 = new DetectLivingFaceRequest.Tasks();
            tasks1.setImageURL(imageUrl);
            tasksList.add(tasks1);
            request.setTaskss(tasksList);
            response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        }
        return response;
    }

    /**
     * 人脸检测   检测是否是人脸图片
     *
     * @param imageUrl
     */
    public DetectFaceResponse DetectFace(String imageUrl) {
        DetectFaceRequest request = new DetectFaceRequest();
        request.setImageURL(imageUrl);
        DetectFaceResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        }
        return response;
    }

    /**
     * 人脸搜索
     * 比较人脸数据库中的人脸数据，返回相近的数据
     *
     * @param dbName
     * @param imageUrl
     */
    public SearchFaceResponse searchFace(String dbName, String imageUrl , Integer limit) {
        if (limit <=0 ){
            throw new FaceBodyBaseException(FaceBodyExceptionMsg.FACEBODY_SEARCHFACE_LIMIT_ERROR);
        }
        SearchFaceResponse response = null;
        SearchFaceRequest request = new SearchFaceRequest();
        request.setDbName(dbName);
        request.setImageUrl(imageUrl);
        //返回最高匹配度的两个值
        request.setLimit(limit);
        try {
            response = client.getAcsResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        }
        return response;
    }

    /**
     * 人脸比对
     * 人脸搜索是1：N，而人脸比对是1：1
     *
     * @param ImageURLA
     * @param ImageURLB
     */
    public CompareFaceResponse faceThan(String ImageURLA, String ImageURLB) {
        CompareFaceRequest request = new CompareFaceRequest();
        CompareFaceResponse response = null;
        request.setImageURLA(ImageURLA);
        request.setImageURLB(ImageURLB);
        try {
            response = client.getAcsResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new FaceBodyBaseException(e.getRequestId(),e.getErrCode(),e.getErrMsg());
        }
        return response;
    }
}
